package com.bestsonic.crawler.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Multi implements Regular<String> {

	private Split startSplit;
	
	private Regex regex;
	
	private Split endSplit;

	public Multi() {
		super();
	}

	public Multi(Split startSplit, Regex regex) {
		super();
		this.startSplit = startSplit;
		this.regex = regex;
	}
	
	public Multi(Regex regex, Split endSplit) {
		super();
		this.regex = regex;
		this.endSplit = endSplit;
	}
	
	public Multi(Split startSplit, Regex regex, Split endSplit) {
		super();
		this.startSplit = startSplit;
		this.regex = regex;
		this.endSplit = endSplit;
	}

	public Split getStartSplit() {
		return startSplit;
	}

	public void setStartSplit(Split startSplit) {
		this.startSplit = startSplit;
	}

	public Regex getRegex() {
		return regex;
	}

	public void setRegex(Regex regex) {
		this.regex = regex;
	}

	public Split getEndSplit() {
		return endSplit;
	}

	public void setEndSplit(Split endSplit) {
		this.endSplit = endSplit;
	}

	public List<String> matches(String content, int flags) {
		List<String> list = new ArrayList<String>();
		if(startSplit != null){
			String[] splitStr = content.split(startSplit.getSplit());
			if(startSplit.getEnd() > splitStr.length || startSplit.getEnd() == 0){
				startSplit.setEnd(splitStr.length);
			}
			splitStr = Arrays.copyOfRange(splitStr,startSplit.getStart(),startSplit.getEnd());
			for(String str : splitStr){
				list.addAll(regex.matches(str, flags));
			}
		}
		else{
			list.addAll(regex.matches(content, flags));
		}
		return list;
	}
	
}
